/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.application;

import com.jb.web.application.configuration.Config;
import com.jb.web.context.WebContext;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HtmlConstants {
    public static final String DOC_TYPE = 
        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 //EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
    public static final String CONTENT_TYPE = 
        "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
    
    public static final StringBuffer PROPERTY_HEADER = 
        new StringBuffer("<script>").append("<!--")
        .append("function selected(formname,fieldname,selectedString)")
        .append("{").append("self.opener.setLovValue(formname, fieldname, selectedString);")
        .append("self.close();").append("}").append("-->").append("</script>");
    
    private static Map scripts =new HashMap();
    private static final StringBuffer _REQUIRED =
        new StringBuffer()        
        .append("<script src=\"jslib/jb.js\" type=\"text/javascript\"  ></script>\n");
    
    private static final StringBuffer REQUIRED =
        new StringBuffer().append("<link rel=\"stylesheet\"  type=\"text/css\" href=\"css/generic.css\" />\n")
        .append("<link rel=\"stylesheet\"  type=\"text/css\" href=\"css/starrating.css\" />\n")
        .append("<link rel=\"stylesheet\"  type=\"text/css\" href=\"css/niftyCorners.css\" />\n")
        .append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/menu.css\" />\n")
        .append("<script src=\"jslib/prototype.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/ef/scriptaculous.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/jb.core.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/jb.selector.2.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/jb.ajax.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/jb.designer.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"htmlarea/htmlarea.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/jb.inspector.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/custom.js\" type=\"text/javascript\"  ></script>\n")
        .append("<script src=\"jslib/jb.calendar.js\" type=\"text/javascript\"  ></script>\n");

    private static final String ERROR_HEADER = 
        new StringBuffer().append("<HTML>").append("<HEAD>")
        .append("<TITLE>JB Fatal Error </TITLE>")
        .append("<META http-equiv=Content-Type content=\"text/html; charset=utf-8\">")
        .append("<META http-equiv=\"Pragma\" content=\"no-cache\"> ")
        .append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/generic.css\" />\n")
        .append("</HEAD>").append("<BODY class=\"errorPage\">").toString();
    private static final String ERROR_TAILER = 
        new StringBuffer().append("</BODY></HTML>").toString();

    public static String RICH_EDITOR="richEditor";
    public static String MENU="menu";
    public static String DESIGNER="designer";
    public static String INSPECTOR="inspector";
    public static String CALENDAR="calendar";
    public static String GEOMAP="geomap";

    static {
        scripts.put("prototype","jslib/prototype.js");
        scripts.put("scriptaculous","jslib/ef/scriptaculous.js");
        scripts.put("core","jslib/jb.core.js");
        scripts.put("ajax","jslib/jb.ajax.js");
        scripts.put(MENU,"jslib/jb.menu.js");
        scripts.put(GEOMAP, "http://maps.google.com/maps?file=api&amp;v=2&amp;key=" + Config.getInstance().getGoogleDevKey());
    }

    public static String getError(String msg) {
        return ERROR_HEADER + msg + ERROR_TAILER;
    }

    public static StringBuffer getInclude(WebContext ctx) {
        List imports = ctx.getIncludedScript();
        Iterator iter=scripts.keySet().iterator();
        StringBuffer include = new StringBuffer(REQUIRED);
        while(iter.hasNext())
        {
            String name = (String)iter.next();
            if(imports.contains(name))
                include.append(scriptTag(getScript(name)));
        }
        return include;//INCLUDE;
    }
    
    private static String getScript(String key) {
        return (String) scripts.get(key);
    }

    private static String scriptTag(String src) {
        return "<script src=\""+ src+ "\" type=\"text/javascript\"  ></script>\n";
    }
    
}
